package com.gko100.dao.basehibernatesupport;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


public class BaseHibernateDaoSupport extends HibernateDaoSupport
{
	protected Log logger;
	public BaseHibernateDaoSupport(){
		logger = LogFactory.getLog(BaseHibernateDaoSupport.class.getName ());
	}
	
	
	
	public List findByPage(final String hql,final int offset,final int pageSize){
		
		List list=this.getHibernateTemplate().executeFind(
			
			new HibernateCallback(){
				public Object doInHibernate(Session session) throws HibernateException, SQLException{
					List result=session.createQuery(hql).setFirstResult(offset).setMaxResults(pageSize).list();
					return result;
				}
			}
		);
		return list;
	}
	 public List findByPage(final String hql,final String value,final int offset,final int pageSize)
	 {
	  List list=this.getHibernateTemplate().executeFind(new HibernateCallback()
	  {
	   public Object doInHibernate(Session session)
	     throws HibernateException, SQLException {
	    List result=session.createQuery(hql).setParameter(0,value).setFirstResult(offset).setMaxResults(pageSize).list();
	    return result;
	   }
	  }
	  );
	  return list;
	 }
	 
	 public List findByPage(final String hql,final Object obj,final int offset,final int pageSize)
	 {
	  List list=this.getHibernateTemplate().executeFind(new HibernateCallback()
	  {
	   public Object doInHibernate(Session session)
	     throws HibernateException, SQLException {
	    List result=session.createQuery(hql).setParameter(0,obj).setFirstResult(offset).setMaxResults(pageSize).list();
	   
	    return result;
	   }
	  }
	  );
	  return list;
	 }
	 
	 
	 public List findByPage(final String hql,final Object obj1, final Object obj2, final int offset,final int pageSize)
	 {
	  List list=this.getHibernateTemplate().executeFind(new HibernateCallback()
	  {
	   public Object doInHibernate(Session session)
	     throws HibernateException, SQLException {
	    List result=session.createQuery(hql).setParameter(0,obj1).setParameter(1, obj2).setFirstResult(offset).setMaxResults(pageSize).list();
	    return result;
	   }
	  }
	  );
	  return list;
	 }
	 
	 
	 public List findByParam(final String hql,final Object obj,final int offset,final int maxResult){
		 
		 List list=this.getHibernateTemplate().executeFind(
				 new HibernateCallback(){
					 public Object doInHibernate(Session session) throws HibernateException, SQLException {
						 List result=session.createQuery(hql).setParameter(0,obj).setFirstResult(offset).setMaxResults(maxResult).list();
						 return result;
					 }
				 }
		  );
		 return list;
 
	 }
	 public List findByParam(final String hql,final Object values[],final int offset,final int maxResult){
		 
		 List list=this.getHibernateTemplate().executeFind(
				 new HibernateCallback(){
					 public Object doInHibernate(Session session) throws HibernateException, SQLException {
						 Query query=session.createQuery(hql);
						    for(int i=0;i<values.length;i++)
						    {
						     query.setParameter(i,values[i]);
						    }
						    List result=query.setFirstResult(offset).setMaxResults(maxResult).list();
						    return result;
					 }
				 }
		  );
		 return list;
 
	 }
	 public long findByParam(final String hql,final Object values[]){
		 
		Long totalCount = (Long) this.getHibernateTemplate().executeFind(
				 new HibernateCallback(){
					 public Object doInHibernate(Session session) throws HibernateException, SQLException {
						 Query query=session.createQuery(hql);
						    for(int i=0;i<values.length;i++)
						    {
						     query.setParameter(i,values[i]);
						    }
						    List result=query.list();
						    return result;
					 }
				 }
		  ).listIterator().next();
		 
		 
		 return totalCount;
	 }
	 
	 
	 
	 
	 
	 
	 
	 
	 public List findByPage(final String hql,final Object[] values,final int offset,final int pageSize){
	  List list=this.getHibernateTemplate().executeFind(new HibernateCallback()
	  {
	   public Object doInHibernate(Session session)
	     throws HibernateException, SQLException {
	    Query query=session.createQuery(hql);
	    for(int i=0;i<values.length;i++)
	    {
	     query.setParameter(i,values[i]);
	    }
	    List result=query.setFirstResult(offset).setMaxResults(pageSize).list();
	    return result;
	   }
	  }
	  );
	  return list;
	 }
}
